package example.demo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class LogAop {
    @Pointcut("@annotation(example.demo.aop.LogAnno)")
    public void logPointCut() {

    }

    @Pointcut("execution(*  example.demo.controller.*.*(..))")
    public void wahaha() {

    }

    //    @After("logPointCut()|| wahaha()")
//    @After("logPointCut()")
    public void logAfter(JoinPoint joinPoint) {

    }

    @Before("logPointCut()")
    public void logBefore(JoinPoint joinPoint) {
        //拿到ThreadLocal的request对象
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        // example.demo.controller.GameController
        String declaringTypeName = signature.getDeclaringTypeName();
        // 请求的url---/api/system/game/dnf
        // 方法限定---public java.lang.String example.demo.controller.GameController.wahaha(java.lang.String)
        // 方法实参值---[null]
        log.info("请求的url---{} 方法限定---{} 方法实参值---{}", servletRequestAttributes.getRequest().getRequestURI(),
                signature.toLongString(),
                Arrays.toString(args));
    }
}
